/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.cards;


import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.library.CardWithList;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.prototypes.ListObject;
import it.gmariotti.cardslib.library.utils.PixelMapUtils;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class GoogleNowNativeWeatherCard extends CardWithList {

    public GoogleNowNativeWeatherCard(Context context) {
        super(context);
    }

    @Override
    protected CardHeader initCardHeader() {

        //Add Header
        CardHeader header = new CardHeader(getContext(), ResourceTable.Layout_carddemo_googlenowweather_native_inner_header);
        header.setPopupMenu(ResourceTable.Layout_popup_item_layout, (CardHeader.OnClickCardHeaderPopupMenuListener) (card1, item) -> {
            switch (Integer.parseInt(card1.getId())) {
                case ResourceTable.Id_menu_item1:
                    //Example: add an item
                    WeatherObject w1 = new WeatherObject(GoogleNowNativeWeatherCard.this);
                    w1.city = "Madrid";
                    w1.temperature = 24;
                    w1.weatherIcon = ResourceTable.Media_ic_action_sun;
                    w1.setObjectId(w1.city);
                    mLinearListAdapter.add(w1);
                    break;
                case ResourceTable.Id_menu_item2:
                    //Example: remove an item
                    mLinearListAdapter.remove(mLinearListAdapter.getItem(0));
                    break;
            }
        });
        header.setTitle("Weather"); //should use ResourceTable.String_
        return header;
    }

    @Override
    protected void initCard() {

        //Set the whole card as swipeable
        setSwipeable(true);
        setOnSwipeListener(card -> new ToastDialog(getContext()).setText("Swipe on " + card.getCardHeader().getTitle()).show());

    }


    @Override
    protected List<ListObject> initChildren() {

        //Init the list
        List<ListObject> mObjects = new ArrayList<ListObject>();

        //Add an object to the list
        WeatherObject w1 = new WeatherObject(this);
        w1.city = "London";
        w1.temperature = 16;
        w1.weatherIcon = ResourceTable.Media_ic_action_cloud;
        w1.setObjectId(w1.city); //It can be important to set ad id
        mObjects.add(w1);

        WeatherObject w2 = new WeatherObject(this);
        w2.city = "Rome";
        w2.temperature = 25;
        w2.weatherIcon = ResourceTable.Media_ic_action_sun;
        w2.setObjectId(w2.city);
        w2.setSwipeable(true);

        //Example onSwipe
        /*w2.setOnItemSwipeListener(new OnItemSwipeListener() {
            @Override
            public void onItemSwipe(ListObject object,boolean dismissRight) {
                Toast.makeText(getContext(), "Swipe on " + object.getObjectId(), Toast.LENGTH_SHORT).show();
            }
        });*/
        mObjects.add(w2);

        WeatherObject w3 = new WeatherObject(this);
        w3.city = "Paris";
        w3.temperature = 19;
        w3.weatherIcon = ResourceTable.Media_ic_action_cloudy;
        w3.setObjectId(w3.city);
        mObjects.add(w3);

        return mObjects;
    }

    @Override
    public Component setupChildView(int childPosition, ListObject object, Component convertView, ComponentContainer parent) {

        //Setup the ui elements inside the item
        Text city = (Text) convertView.findComponentById(ResourceTable.Id_carddemo_weather_city);
        Image icon = (Image) convertView.findComponentById(ResourceTable.Id_carddemo_weather_icon);
        Text temperature = (Text) convertView.findComponentById(ResourceTable.Id_carddemo_weather_temperature);

        //Retrieve the values from the object
        WeatherObject weatherObject = (WeatherObject) object;
        try {
            icon.setImageElement(PixelMapUtils.prepareElementById(getContext(), weatherObject.weatherIcon));
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        city.setText(weatherObject.city);
        temperature.setText(weatherObject.temperature + weatherObject.temperatureUnit);

        return convertView;
    }

    @Override
    public int getChildLayoutId() {
        return ResourceTable.Layout_carddemo_googlenowweather_native_inner_main;
    }


    // -------------------------------------------------------------
    // Weather Object
    // -------------------------------------------------------------

    public class WeatherObject extends DefaultListObject {

        public String city;
        public int weatherIcon;
        public int temperature;
        public String temperatureUnit = "°C";

        public WeatherObject(Card parentCard) {
            super(parentCard);
            init();
        }

        private void init() {
            //OnClick Listener
            setOnItemClickListener((parent, view, position, object) -> new ToastDialog(getContext()).setText("Click on " + getObjectId()).show());

            //OnItemSwipeListener
            setOnItemSwipeListener((object, dismissRight) -> new ToastDialog(getContext()).setText("Swipe on " + getObjectId()).show());
        }

    }


}
